package com.ge.modules.api.secondController;

import com.ge.common.annotation.LoginStoreApi;
import com.ge.common.utils.DeleteType;
import com.ge.common.utils.PageUtils;
import com.ge.common.utils.Query;
import com.ge.common.utils.R;
import com.ge.modules.api.apiDTO.TbPostDto;
import com.ge.modules.api.service.*;
import com.ge.modules.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 帖子表
 *
 * @author lanyonglin
 * @email www.baidu.com
 * @date 2018-07-17 15:12:57
 */
@RestController
@RequestMapping("/api/store/post")
public class TbStorePostController {
    @Autowired
    private TbPostService postService;

    @Autowired
    private TbPostLikeService tbPostLikeService;

    @Autowired
    private TbPostReadService tbPostReadService;

    @Autowired
    private TbPostCommentService tbPostCommentService;

    @Autowired
    private TbUserService tbUserService;

    @Autowired
    private TbStoreService tbStoreService;

    /**
     * 查询帖子列表 根据用户类型
     *
     * @param tbStoreEntity
     * @param params
     * @return
     */
    @RequestMapping("/list")
    public R list(@LoginStoreApi TbStoreEntity tbStoreEntity, @RequestParam Map<String, Object> params) {
        params.put("deleteType", DeleteType.NOT_DELETE.getKey());
        //判断是否查询自己帖子
//        Integer ifSelf = (Integer) params.get("ifSelf");
        String s = (String) params.get("ifSelf");
        Integer ifSelf = Integer.valueOf(s);
        if (ifSelf == 1) {
            params.put("userId", tbStoreEntity.getStoreId());
        }
        //查询列表数据
        Query query = new Query(params);
        List<TbPostEntity> postList = postService.queryList(query);
        int total = postService.queryTotal(query);
        //查询数据的点赞数 评论数 查看数
        List<TbPostDto> tbPostDtoList = new ArrayList<>();
        for (int i = 0; i < postList.size(); i++) {
            TbPostDto tbPostDto = postList.get(i).toDto();
            //查询店铺
            TbStoreEntity tbStoreEntity1 = tbStoreService.queryByStoreId(tbPostDto.getUserId());
            tbPostDto.setNickname(tbStoreEntity1.getStoreName());
            tbPostDto.setHeadPicture(tbStoreEntity1.getBackgroundPicture());
            //查看自己是否点赞
            TbPostLikeEntity tbPostLikeEntity = new TbPostLikeEntity();
            tbPostLikeEntity.setPostIdSlave(tbPostDto.getId());
            tbPostLikeEntity.setUserIdMaster(tbStoreEntity.getStoreId());
            tbPostLikeEntity.setUserType(tbStoreEntity.getUserType());
            if (tbPostLikeService.queryByObject(tbPostLikeEntity) != null) {
                tbPostDto.setIfLike(1);
            }
            int comments = 0;
            int reads = 0;
            int likes = 0;
            //查看评论数
            Map<String, Object> commentMap = new HashMap<>();
            commentMap.put("postIdSlave", tbPostDto.getId());
            List<TbPostCommentEntity> tbPostCommentEntityList = tbPostCommentService.queryList(commentMap);
            comments = tbPostCommentEntityList.size();
            tbPostDto.setComments(comments);
            //查看查看数
            Map<String, Object> readMap = new HashMap<>();
            readMap.put("postIdSlave", tbPostDto.getId());
            List<TbPostReadEntity> tbPostReadEntityList = tbPostReadService.queryList(readMap);
            reads = tbPostReadEntityList.size();
            tbPostDto.setReads(reads);
            //查看点赞数
            Map<String, Object> likeMap = new HashMap<>();
            likeMap.put("postIdSlave", tbPostDto.getId());
            List<TbPostLikeEntity> tbPostLikeEntityList = tbPostLikeService.queryList(likeMap);
            likes = tbPostLikeEntityList.size();
            tbPostDto.setLikes(likes);
            tbPostDtoList.add(tbPostDto);
        }
        PageUtils pageUtil = new PageUtils(tbPostDtoList, total, query.getLimit(), query.getPage());
        return R.ok().put("data", pageUtil);
    }

    /**
     * 查询帖子详细信息 添加查看数
     *
     * @param tbStoreEntity
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@LoginStoreApi TbStoreEntity tbStoreEntity, @PathVariable("id") Integer id) {
        //判断当前帖子是否被当前用户阅读过
        TbPostReadEntity tbPostReadEntity = new TbPostReadEntity();
        tbPostReadEntity.setUserIdMaster(tbStoreEntity.getStoreId());
        tbPostReadEntity.setUserType(tbStoreEntity.getUserType());
        tbPostReadEntity.setPostIdSlave(id);
        //如果不存在 就添加阅读数
        if (tbPostReadService.queryByObject(tbPostReadEntity) == null) {
            tbPostReadEntity.setCreateTime(new Date());
            tbPostReadService.save(tbPostReadEntity);
        }
        TbPostEntity post = postService.queryObject(id);
        return R.ok().put("data", post);
    }

    /**
     * 添加帖子
     *
     * @param tbStoreEntity
     * @param post
     * @return
     */
    @PostMapping("/save")
    public R save(@LoginStoreApi TbStoreEntity tbStoreEntity, TbPostEntity post) {
        post.setUserId(tbStoreEntity.getStoreId());
        post.setCreateTime(new Date());
        post.setDeleteType(DeleteType.NOT_DELETE.getKey());
        post.setUpdateTime(new Date());
        post.setUserType(tbStoreEntity.getUserType());
        postService.save(post);
        return R.ok();
    }

    /**
     * 删除帖子
     *
     * @param ids
     * @return
     */
    @RequestMapping("/delete")
    public R delete(Integer[] ids) {
        postService.deleteBatch(ids);
        return R.ok();
    }

}
